/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jxtafileshare;

import net.jxta.document.AdvertisementFactory;
import net.jxta.endpoint.Message;
import net.jxta.endpoint.ByteArrayMessageElement;
import net.jxta.id.IDFactory;
import net.jxta.pipe.OutputPipe;
import net.jxta.pipe.OutputPipeEvent;
import net.jxta.pipe.OutputPipeListener;
import net.jxta.pipe.PipeID;
import net.jxta.pipe.PipeService;
import net.jxta.platform.NetworkManager;
import net.jxta.protocol.PipeAdvertisement;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;
import net.jxta.peergroup.PeerGroup;
/**
 *
 * @author Danny
 */
public class ShareClient extends Thread implements OutputPipeListener {
    public final static String MESSAGE_NAME_SPACE = "FileShare";
    private boolean waitForRendezvous = false;
    private PipeService pipeService;
    private PipeAdvertisement pipeAdv;
    private OutputPipe outputPipe;
    private NetworkManager manager;
    private PeerGroup peerGroup;
    private File file = null;
    private final Object lock = new String("lock");

    public ShareClient(boolean waitForRendezvous,
            PeerGroup peerGroup, NetworkManager manager, File file) {
        this.waitForRendezvous = waitForRendezvous;
        this.manager = manager;
        this.file = file;
        if(peerGroup != null) {
            this.peerGroup = peerGroup;
        } else {
            System.out.println("Peer Group does not exist");
            System.exit(-1);
        }

        pipeService = this.peerGroup.getPipeService();
    }

    @Override
    public synchronized void run() {
        try {
            if(waitForRendezvous) {
                System.out.println("Waiting for Rendezvous Connection");

                manager.waitForRendezvousConnection(0);
                System.out.println("Connected to Rendezvous, attempting" +
                        "to create an OutputPipe");
            }

            pipeAdv = FileFinder.createFileAdvertisement(file,
                    peerGroup.getPeerGroupID());
            pipeService.createOutputPipe(pipeAdv, this);
            try {
                synchronized(lock) {
                    lock.wait();
                }
            } catch(InterruptedException e) {
                System.out.println("Thread interrupted");
            }
        } catch(IOException e) {
            System.out.println("OutputPipe creation failure");
            e.printStackTrace();
            System.exit(-1);
        }
    }

    public void outputPipeEvent(OutputPipeEvent event) {
        System.out.println("Received the output pipe resolution event");

        outputPipe = event.getOutputPipe();

        Message msg;
        try {
            System.out.println("Sending message");

            msg = new Message();
            if(file != null) {
                Messages.addByteArrayToMessage(msg, MESSAGE_NAME_SPACE, file.getName(), FileFinder.getBytes(file), true);
                outputPipe.send(msg);
                System.out.println("Message sent");
            }
        } catch(IOException e) {
            System.out.println("failed to send message");
            e.printStackTrace();
            System.exit(-1);
        }
        finish();
    }

    public void finish() {
        outputPipe.close();
        synchronized(lock) {
            lock.notify();
        }
    }
}
